#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include"Tool.hpp"
#include"Task.hpp"

int main()
{
	Tool tool;
	tool.PushFunc(Downloading);
	tool.PushFunc(Printing);
	tool.PushFunc(Erase);

	pid_t id = fork();
	if(id<0)
	{
		printf("子进程创建失败！\n");
		return 1;
	}
	else if(id == 0)
	{
		//子进程
		int cnt = 10;
		while(cnt--)
		{
			printf("子进程正在运行\n");
			sleep(1);
		}
		exit(0);
	}
	else
	{
		//父进程
		while(1)
		{
			pid_t rid = waitpid(id,NULL,WNOHANG);
			if(rid == id)
			{
				printf("wait child success!\n");
				sleep(1);
				break;
			}
			else if(rid == 0)
			{
				
				printf("Child proc. is still running...\n");
				tool.Execute();			
				sleep(1);
			}
			else
			{
				perror("waitpid()");
				exit(EXIT_FAILURE);
			}
	        } 
	}

	return 0;
}

